﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DataChangeLog                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/5/7 14:15:03                        
*└────────────────────────────────────────────────┘
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Data
{
    public class DataChangeLog
    {
        public DataTable GetDataChangeTable(string strTbName)
        {
            try
            {
                DataTable dt;
                //string sql = "SELECT TOP " + AppConfig.UpLoadMaxDataLength.ToString() + " 编码,表名,主键,操作,是否处理,插入时间 FROM dbo.TDataChangeLog WHERE 是否处理=0 AND 表名='" + strTbName + "' ";
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"SELECT TOP " + AppConfig.UpLoadMaxDataLength.ToString() + " 编码,表名,主键,操作,是否处理,插入时间 FROM dbo.TDataChangeLog WHERE 是否处理=0 AND 表名='" + strTbName + "' ");
                return DataSetUtility.GetDataSetFromTold(strSql).Tables[0];
                //using (dt = SqlHelper.ExecuteDataset(SqlHelper.DataCenterConnString, CommandType.Text, sql, null).Tables[0])
                //{
                //    return dt;
                //}
            }
            catch (Exception e)
            {
                LogUtility.Error("DataChangeLog/GetDataChangeTable" + strTbName, "获取数据改变日志信息出错！ " + e.Message);
                return null;
            }
        }


        /// <summary>
        /// 更新处理标识
        /// </summary>
        /// <param name="ambCode"></param>
        /// <returns></returns>
        public bool UpdateDataChangeSign(string strPKCode)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" UPDATE TDataChangeLog SET 是否处理 = 1");
                strSql.Append(" WHERE  编码 = '" + strPKCode + "' ");

                int intRow = DataSetUtility.GetNonQueryFromTold(strSql);
                if (!intRow.Equals(0))
                { return true; }
                else
                { return false; }
            }
            catch (Exception e)
            {

                LogUtility.Error("DataChangeLog/UpdateDataChangeSign", "更新处理标识出错！" + e.Message);
                return false;
            }

        }


        /// <summary>
        /// 大批量更新处理标识
        /// </summary>
        /// <param name="ambCode"></param>
        /// <returns></returns>
        public bool BulkUpdateDataChangeSign(string pkClauses)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" UPDATE TDataChangeLog SET 是否处理 = 1");
            strSql.Append(" WHERE  编码 in (" + pkClauses + ") ");
            int intRow = DataSetUtility.GetNonQueryFromTold(strSql);
            if (!intRow.Equals(0))
            { return true; }
            else
            { return false; }
        }


        /// <summary>
        /// 清除数据改变表缓存数据
        /// </summary>
        /// <returns>处理结果</returns>
        public bool DeleteDataChangeLog()
        {
            try
            {
                StringBuilder strSql = new StringBuilder(@" DELETE FROM dbo.TDataChangeLog WHERE 是否处理 = 1 And 插入时间 <= '" + DateTime.Now.AddHours(-1).ToString() + "' ");
                int intRow = DataSetUtility.GetNonQueryFromTold(strSql);
                if (!intRow.Equals(0))
                { return true; }
                else
                { return false; }
            }
            catch (Exception e)
            {
                LogUtility.Error("DataChangeLog/DeleteDataChangeLog", "清除数据改变表缓存数据出错！ " + e.Message);
                return false;
            }
        }

    }
}
